# IndexerThread - Walks directories recursively and adds files to cache.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import time
import threading

from backend.components.mediacache.image_cache import ImageCache
from backend.components.mediacache.music_cache import MusicCache
from backend.components.mediacache.video_cache import VideoCache

class IndexerThread(threading.Thread):
    """
    IndexerThread
    
    This is a thread that indexes all the media into media cache. Each media
    type has it's own indexing thread. Each thread therefore has a media type
    which is set with setCacheType() -method.
    """
    
    def __init__(self, logger, configuration):
        """
        Create a new indexer thread.
        @param logger: Logger object
        @param configuration: Configuration object
        """
        threading.Thread.__init__(self)
        self.setName("IndexerThread")
        self.logger = logger
        self.cache_type = None
        self.root_folders = None # Root folders (Root folders that should be indexed. Get from content.conf)
        self.configuration = configuration
        
    def setCacheType(self, cache_type):
        """
        Set cache type  for this indexer. Allowed values are 'image','music','video'
        @param cache_type: Cache type (String)
        """
        if cache_type != "image" and cache_type != "music" and cache_type != "video":
            raise Exception("Illegal cache type.")
        else:
            self.cache_type = cache_type

    def setFolders(self, folders):
        """
        Set root folders for this indexer
        @param folders: String array that contains folder paths
        """
        self.root_folders = folders
        
    def run(self):
        """
        Walk root directories recursively and add all found files to the cache.
        """
        if self.cache_type == "image":
            cache = ImageCache(self.logger, self.configuration)
        elif self.cache_type == "music":
            cache = MusicCache(self.logger, self.configuration)
        elif self.cache_type == "video":
            cache = VideoCache(self.logger, self.configuration)
        
        if self.root_folders == None:
            return
            
        for element in self.root_folders:
            cache.addDirectory(element)
